package com.ws.wsdemo2.util;

import com.ws.wsdemo2.model.QuotePeriod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.support.CronSequenceGenerator;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * 
 * 功能描述： 行情时间周期
 * @作者 luozhh	创建时间： 2016年5月27日 下午1:37:19
 *
 */
@Component
@Slf4j
public class QuotePeriodUtil {

	private static CronSequenceGenerator oneMinuteTrigger = new CronSequenceGenerator("0 0/1 * * * ? ");
	private static CronSequenceGenerator fiveMinuteTrigger = new CronSequenceGenerator("0 0/5 *  * * ? ");
	private static CronSequenceGenerator fifteenMinuteTrigger = new CronSequenceGenerator("0 0/15 *  * * ? ");
	private static CronSequenceGenerator thirtyMinuteTrigger = new CronSequenceGenerator("0 0/30 *  * * ? ");
	private static CronSequenceGenerator fourHourTrigger = new CronSequenceGenerator("0 0 0/4  * * ? ");
	private static CronSequenceGenerator sixtyMinuteTrigger = new CronSequenceGenerator("0 0/60 *  * * ? ");
	// 每天上午6：00触发
	private static CronSequenceGenerator dayTrigger = new CronSequenceGenerator("0 0 6 * * ?");
	// 每个星期一上午6点触发
	private static CronSequenceGenerator weekTrigger = new CronSequenceGenerator("0 0 6 ? * MON");
	// 表示在每月的1日的上午6点触发
	private static CronSequenceGenerator monthTrigger = new CronSequenceGenerator("0 0 6 1 * ?");
/*
	@Autowired
	private ParameterCache parameterCache;*/

	public QuotePeriod getPeriod(String exchangeTag, PeriodType type, Date dateTime, String symbol) {
		QuotePeriod period = null;
		Date nextExecutionTime = null;
		switch (type) {
		case ONE_MINUTE:
			nextExecutionTime = oneMinuteTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 1 * 60 * 1000, nextExecutionTime.getTime(), type);
			break;
		case FIVE_MINUTE:
			nextExecutionTime = fiveMinuteTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 5 * 60 * 1000, nextExecutionTime.getTime(), type);
			break;
		case FIFTEEN_MINUTE:
			nextExecutionTime = fifteenMinuteTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 15 * 60 * 1000, nextExecutionTime.getTime(), type);
			break;
		case THIRTY_MINUTE:
			nextExecutionTime = thirtyMinuteTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 30 * 60 * 1000, nextExecutionTime.getTime(), type);
			break;
		case SIXTY_MINUTE:
			nextExecutionTime = sixtyMinuteTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 60 * 60 * 1000, nextExecutionTime.getTime(), type);
			break;
		case FOUR_HOUR:
			nextExecutionTime = fourHourTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 4 * 60 * 60 * 1000, nextExecutionTime.getTime(), type);
			break;
		case DAY:
			/*if(parameterCache.getTradeDayStartTime(exchangeTag, symbol) != null
					&& parameterCache.getTradeDayEndTime(exchangeTag, symbol) != null
					&& dateTime.getTime() >= parameterCache.getTradeDayStartTime(exchangeTag, symbol)
					&& dateTime.getTime() < parameterCache.getTradeDayEndTime(exchangeTag, symbol)) {
				period = new QuotePeriod(parameterCache.getTradeDayStartTime(exchangeTag, symbol), parameterCache.getTradeDayEndTime(exchangeTag, symbol), type);
			} else if(parameterCache.getLastTradeDayStartTime(exchangeTag, symbol) != null
					&& parameterCache.getLastTradeDayEndTime(exchangeTag, symbol) != null
					&& dateTime.getTime() >= parameterCache.getLastTradeDayStartTime(exchangeTag, symbol)
					&& dateTime.getTime() < parameterCache.getLastTradeDayEndTime(exchangeTag, symbol)) {
				period = new QuotePeriod(parameterCache.getLastTradeDayStartTime(exchangeTag, symbol), parameterCache.getLastTradeDayEndTime(exchangeTag, symbol), type);
			} else {
				log.warn("非交易日接收到行情, 交易日起始时间：{} {}，上一个交易日起始日期：{} {}", parameterCache.getTradeDayStartTime(exchangeTag, symbol), parameterCache.getTradeDayEndTime(exchangeTag, symbol), parameterCache.getLastTradeDayStartTime(exchangeTag, symbol), parameterCache.getLastTradeDayEndTime(exchangeTag, symbol));
			}*/
			break;
		case WEEK:
			nextExecutionTime = weekTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 7 * 24 * 60 * 60 * 1000, nextExecutionTime.getTime(), type);
			break;
		case MONTH:
			nextExecutionTime = monthTrigger.next(dateTime);
			// 获取当月1号上午6点
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date(nextExecutionTime.getTime()));
			calendar.add(Calendar.MONTH, -1);// 取上个月
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 6);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			period = new QuotePeriod(calendar.getTimeInMillis(), nextExecutionTime.getTime(), type);
			break;
		default:
			nextExecutionTime = thirtyMinuteTrigger.next(dateTime);
			period = new QuotePeriod(nextExecutionTime.getTime() - 30 * 60 * 1000, nextExecutionTime.getTime(), PeriodType.THIRTY_MINUTE);
		}

		return period;
	}

	public enum PeriodType {
		/** 分时 */
		ONE_MINUTE("1"),

		/** 5分钟 */
		FIVE_MINUTE("5"),

		/** 15分钟 */
		FIFTEEN_MINUTE("15"),

		/** 30分钟 */
		THIRTY_MINUTE("30"),

		/** 60分钟 */
		SIXTY_MINUTE("60"),

		/** 天 */
		DAY("day"),

		/** 4小时*/
		FOUR_HOUR("4"),

		/** 周 */
		WEEK("week"),

		/** 月 */
		MONTH("month");

		private String index;

		// 构造方法
		private PeriodType(String index) {
			this.index = index;
		}

		public String getIndex() {
			return index;
		}
	}

	public static void main(String[] args) {

	}

}
